
<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      {{- range .Fields}}
          {{- $dictType := .DictType}}
          {{- $comment := .ColumnComment}}
          {{- if eq .HtmlType "input"}}
            <el-form-item label="{{$comment}}" prop="{{.JavaField}}">
              <el-input
                v-model="queryParams.{{.JavaField}}"
                placeholder="请输入{{$comment}}"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          {{- else if or (eq .HtmlType "select") (eq .HtmlType "radio")}}
            <el-form-item label="{{$comment}}" prop="{{.JavaField}}">
              <el-select v-model="queryParams.{{.JavaField}}" placeholder="请选择{{$comment}}" clearable>
                {{- if ne $dictType ""}}
                  <el-option
                    v-for="dict in dict.type.{{$dictType}}"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                {{- else}}
                  <el-option label="请选择字典生成" value="" />
                {{- end}}
              </el-select>
            </el-form-item>
          {{- else if eq .HtmlType "datetime"}}
            {{- if eq .QueryType "BETWEEN"}}
              {{$AttrName := printf "%s%s" (upper (string (index .JavaField 0))) (substr .JavaField 1)}}
              <el-form-item label="{{$comment}}">
                <el-date-picker
                  v-model="daterange{{$AttrName}}"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                ></el-date-picker>
              </el-form-item>
            {{- else}}
              <el-form-item label="{{$comment}}" prop="{{.JavaField}}">
                <el-date-picker clearable
                  v-model="queryParams.{{.JavaField}}"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择{{$comment}}">
                </el-date-picker>
              </el-form-item>
            {{- end}}
          {{- end}}
      {{- end}}
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="{{.BusinessName}}List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      {{- range .Fields}}
        {{- $javaField := .JavaField}}
        {{- $comment := .ColumnComment}}
        {{- if eq .IsPk "1"}}
          <el-table-column label="{{$comment}}" align="center" prop="{{$javaField}}" />
        {{- else if eq .IsList "1"}}
          {{- if eq .HtmlType "datetime"}}
            <el-table-column label="{{$comment}}" align="center" prop="{{$javaField}}" width="180">
              <template slot-scope="scope">
                <span>{{ printf "parseTime(scope.row.%s, '{y}-{m}-{d}')" $javaField }}</span>
              </template>
            </el-table-column>
          {{- else if eq .HtmlType "imageUpload"}}
            <el-table-column label="{{$comment}}" align="center" prop="{{$javaField}}" width="100">
              <template slot-scope="scope">
                <image-preview :src="scope.row.{{$javaField}}" :width="50" :height="50"/>
              </template>
            </el-table-column>
          {{- else if ne .DictType ""}}
            <el-table-column label="{{$comment}}" align="center" prop="{{$javaField}}">
              <template slot-scope="scope">
                {{- if eq .HtmlType "checkbox"}}
                  <dict-tag :options="dict.type.{{.DictType}}" :value="scope.row.{{$javaField}}? scope.row.{{$javaField}}.split(',') : []"/>
                {{- else}}
                  <dict-tag :options="dict.type.{{.DictType}}" :value="scope.row.{{$javaField}}"/>
                {{- end}}
              </template>
            </el-table-column>
          {{- else}}
            <el-table-column label="{{$comment}}" align="center" prop="{{$javaField}}" />
          {{- end}}
        {{- end}}
      {{- end}}
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        {{- range .Fields}}
          {{- if eq .IsInsert "1" }}
            {{- if ne .IsPk "1"}}
              {{- $field := .JavaField}}
              {{- $comment := .ColumnComment}}
              {{- $dictType := .DictType}}
              {{- if eq .HtmlType "input"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <el-input v-model="form.{{$field}}" placeholder="请输入{{$comment}}" />
                </el-form-item>
              {{- else if eq .HtmlType "imageUpload"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <image-upload v-model="form.{{$field}}"/>
                </el-form-item>
              {{- else if eq .HtmlType "fileUpload"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <file-upload v-model="form.{{$field}}"/>
                </el-form-item>
              {{- else if eq .HtmlType "editor"}}
                <el-form-item label="{{$comment}}">
                  <editor v-model="form.{{$field}}" :min-height="192"/>
                </el-form-item>
              {{- else if eq .HtmlType "select"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <el-select v-model="form.{{$field}}" placeholder="请选择{{$comment}}">
                    {{- if ne $dictType ""}}
                      <el-option
                        v-for="dict in dict.type.{{$dictType}}"
                        :key="dict.value"
                        :label="dict.label"
                        {{- if or (eq .JavaType "Integer") (eq .JavaType "Long")}}:value="parseInt(dict.value)"{{- else}}:value="dict.value"{{- end}}
                      ></el-option>
                    {{- else}}
                      <el-option label="请选择字典生成" value="" />
                    {{- end}}
                  </el-select>
                </el-form-item>
              {{- else if eq .HtmlType "checkbox"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <el-checkbox-group v-model="form.{{$field}}">
                    {{- if ne $dictType ""}}
                      <el-checkbox
                        v-for="dict in dict.type.{{$dictType}}"
                        :key="dict.value"
                        :label="dict.value">
                        {{printf "dict.label"}}
                      </el-checkbox>
                    {{- else}}
                      <el-checkbox>请选择字典生成</el-checkbox>
                    {{- end}}
                  </el-checkbox-group>
                </el-form-item>
              {{- else if eq .HtmlType "radio"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <el-radio-group v-model="form.{{$field}}">
                    {{- if ne $dictType ""}}
                      <el-radio
                        v-for="dict in dict.type.{{$dictType}}"
                        :key="dict.value"
                        {{- if or (eq .JavaType "Integer") (eq .JavaType "Long")}}:label="parseInt(dict.value)"{{- else}}:label="dict.value"{{- end}}
                      >
                        {{printf "dict.label"}}
                      </el-radio>
                    {{- else}}
                      <el-radio label="1">请选择字典生成</el-radio>
                    {{- end}}
                  </el-radio-group>
                </el-form-item>
              {{- else if eq .HtmlType "datetime"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <el-date-picker clearable
                    v-model="form.{{$field}}"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择{{$comment}}">
                  </el-date-picker>
                </el-form-item>
              {{- else if eq .HtmlType "textarea"}}
                <el-form-item label="{{$comment}}" prop="{{$field}}">
                  <el-input v-model="form.{{$field}}" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              {{- end}}
            {{- end}}
          {{- end}}
        {{- end}}
      </el-form>
        <div slot="footer" class="dialog-footer" align = "center">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
{{- $className := replaceC5 (.ClassName)}}
import { list{{$className}}, get{{$className}}, del{{$className}}, add{{$className}}, update{{$className}} } from "@/api/{{.ModuleName}}/{{.BusinessName}}";

export default {
  name: "{{$className}}",
  {{- if ne .Options ""}}
  dicts: [{{.Options}}],
  {{- end}}
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // {{.FunctionName}}表格数据
      {{.BusinessName}}List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      {{- range .Fields}}
        {{- if and (eq .HtmlType "datetime") (eq .QueryType "BETWEEN")}}
          {{- $AttrName := printf "%s%s" (upper (string (index .JavaField 0))) (substr .JavaField 1)}}
          // 时间范围
          daterange{{$AttrName}}: [],
        {{- end}}
      {{- end}}
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        sortField: "id",
        sortOrder: "descending",
        {{- $lastIndex := sub (len .Fields) 1}}
        {{- range $index, $field := .Fields}}
          {{- if eq $field.IsQuery "1"}}
            {{$field.JavaField}}: null{{- if ne $index $lastIndex}},{{- end}}
          {{- end}}
        {{- end}}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        {{- $lastIndex := sub (len .Fields) 1}}
        {{- range $index, $field := .Fields}}
          {{- if eq $field.IsRequired "1"}}
            {{$field.JavaField}}: [
              { required: true, message: "{{$field.ColumnComment}}不能为空", trigger: {{- if or (eq $field.HtmlType "select") (eq $field.HtmlType "radio")}}"change"{{- else}}"blur"{{- end}} }
            ]{{- if ne $index $lastIndex}},{{- end}}
          {{- end}}
        {{- end}}
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询{{.FunctionName}}列表 */
    getList() {
      this.loading = true;
      {{- range .Fields}}
        {{- if and (eq .HtmlType "datetime") (eq .QueryType "BETWEEN")}}
          this.queryParams.params = {};
          {{break}}
        {{- end}}
      {{- end}}
      {{- range .Fields}}
        {{- if and (eq .HtmlType "datetime") (eq .QueryType "BETWEEN")}}
          {{- $AttrName := printf "%s%s" (upper (string (index .JavaField 0))) (substr .JavaField 1)}}
          if (null!= this.daterange{{$AttrName}} && ''!= this.daterange{{$AttrName}}) {
            this.queryParams.params["begin{{$AttrName}}"] = this.daterange{{$AttrName}}[0];
            this.queryParams.params["end{{$AttrName}}"] = this.daterange{{$AttrName}}[1];
          }
        {{- end}}
      {{- end}}
      list{{$className}}(this.queryParams).then(response => {
        this.{{.BusinessName}}List = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        {{- $lastIndex := sub (len .Fields) 1}}
        {{- range $index, $field := .Fields}}
          {{- if eq $field.HtmlType "checkbox"}}
            {{$field.JavaField}}: []{{- if ne $index $lastIndex}},{{- end}}
          {{- else}}
            {{$field.JavaField}}: null{{- if ne $index $lastIndex}},{{- end}}
          {{- end}}
        {{- end}}
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
        /** 重置按钮操作 */
        resetQuery() {
          {{- range .Fields}}
            {{- if and (eq .HtmlType "datetime") (eq .QueryType "BETWEEN")}}
              {{$AttrName := printf "%s%s" (upper (string (index .JavaField 0))) (substr .JavaField 1)}}
              this.daterange{{$AttrName}} = [];
            {{- end}}
          {{- end}}
          this.resetForm("queryForm");
          this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
          let pkField = "";
          {{- range .Fields}}
            {{- if eq .IsPk "1"}}
              pkField = "{{.JavaField}}";
            {{- end}}
          {{- end}}
          this.ids = selection.map(item => item[pkField])
          this.single = selection.length!== 1
          this.multiple =!selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
          this.reset();
          this.open = true;
          this.title = "添加{{.FunctionName}}";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
          this.reset();
          let pkField = "";
          {{- range .Fields}}
            {{- if eq .IsPk "1"}}
              pkField = "{{.JavaField}}";
            {{- end}}
          {{- end}}
          const id = row[pkField] || this.ids
          get{{$className}}(id).then(response => {
            this.form = response.data;
            {{- range .Fields}}
              {{- if eq .HtmlType "checkbox"}}
                this.form.{{.JavaField}} = this.form.{{.JavaField}}? this.form.{{.JavaField}}.split(",") : [];
              {{- end}}
            {{- end}}
            this.open = true;
            this.title = "修改{{.FunctionName}}";
          });
        },
        /** 提交按钮 */
        submitForm() {
          this.$refs["form"].validate(valid => {
            if (valid) {
              {{- range .Fields}}
                {{- if eq .HtmlType "checkbox"}}
                  this.form.{{.JavaField}} = this.form.{{.JavaField}}.join(",");
                {{- end}}
              {{- end}}
              let pkField = "";
              {{- range .Fields}}
                {{- if eq .IsPk "1"}}
                  pkField = "{{.JavaField}}";
                {{- end}}
              {{- end}}
              if (this.form[pkField]!= null) {
                update{{$className}}(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                add{{$className}}(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
          });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
          let pkField = "";
          {{- range .Fields}}
            {{- if eq .IsPk "1"}}
              pkField = "{{.JavaField}}";
            {{- end}}
          {{- end}}
          const ids = row[pkField] || this.ids;
          this.$modal.confirm('是否确认删除{{.FunctionName}}编号为"' + ids + '"的数据项？').then(() => {
            return del{{$className}}(ids);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
          this.download('{{.ModuleName}}/{{.BusinessName}}/export', {
            ...this.queryParams
          }, '{{.BusinessName}}_' + new Date().getTime() + '.xlsx');
        }
      }
    };
    </script>

    <style scoped>
    /* 可以添加一些自定义的样式 */
    .app-container {
      padding: 20px;
    }
    </style>
